package com.avril.db;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import com.avril.constants.DBConstants;
import com.avril.entity.GoodsType;
import com.avril.util.ErrorManager;

public class GoodsTypeDao {
	SqlManager manage = null;

	public GoodsTypeDao() {
		super();
		manage = SqlManager.createInstance();
		manage.connectDB();
	}

	public boolean addGoodsType(GoodsType goodsType) {
		boolean result = false;
		try {
			Object[] params = new Object[] { goodsType.getTypeName() };
			String sql = "insert into type(typename) values(?)";
			result = manage.executeUpdate(sql, params, DBConstants.PSTM_TYPE);
			manage.closeDB();
		} catch (Exception e) {
			ErrorManager.printError("GoodsTypeDao.addGoodsType", e);
		}
		return result;
	}

	public Vector<GoodsType> getAllGoodsType(String condition) {
		String sql = "select * from type";
		if(condition.equals(DBConstants.AVAILABLE)){
			sql = "select * from type t,goods g where t.typeid = g.typeid and g.amount > 0";
		}
		Vector<GoodsType> list = new Vector<GoodsType>();
		ResultSet rs = manage.executeQuery(sql, null, DBConstants.PSTM_TYPE);
		try {
			while (rs.next()) {
				GoodsType goodsType = new GoodsType();
				goodsType.setTypeId(rs.getInt(DBConstants.TYPE_ID));
				goodsType.setTypeName(rs.getString(DBConstants.TYPE_NAME));
				list.add(goodsType);
			}
		} catch (SQLException e) {
			ErrorManager.printError("GoodsTypeDao.getAllGoodsType", e);
		}
		return list;
	}
	
	public Vector<String> getAllGoodsTypeName(String condition) {
		String sql = "select typename from type";
		if(condition.equals(DBConstants.AVAILABLE)){
			sql = "select distinct t.typename from type t,goods g where t.typeid = g.typeid and g.amount > 0";
		}
		Vector<String> list = new Vector<String>();
		ResultSet rs = manage.executeQuery(sql, null, DBConstants.PSTM_TYPE);
		try {
			while (rs.next()) {
				list.add(rs.getString(DBConstants.TYPE_NAME));
			}
		} catch (SQLException e) {
			ErrorManager.printError("GoodsTypeDao.getAllGoodsTypeName", e);
		}
		return list;
	}

	public GoodsType getGoodsTypeIdByName(String typeName) {
		GoodsType goodsType = new GoodsType();
		try {
			String sql = "select * from type where typename=?";
			Object[] params = new Object[] { typeName };
			ResultSet rs = manage.executeQuery(sql, params,
					DBConstants.PSTM_TYPE);
			rs.next();
			goodsType.setTypeId(rs.getInt(DBConstants.TYPE_ID));
			goodsType.setTypeName(rs.getString(DBConstants.TYPE_NAME));
			manage.closeDB();
		} catch (Exception e) {
			ErrorManager.printError("GoodsTypeDao.getGoodsTypeIdByName", e);
		}
		return goodsType;
	}
	
	public boolean isExist(String goodsTypeName) {
		boolean result = false;
		try {
			Object[] params = new Object[] { goodsTypeName};
			String sql = "select * from type where typename = ?";
			ResultSet rs = manage.executeQuery(sql, params,
					DBConstants.PSTM_TYPE);
			while (rs.next())
				result = true;
			manage.closeDB();
		} catch (Exception e) {
			ErrorManager.printError("GoodsTypeDao.isExist", e);
		}
		return result;
	}

	public String getGoodsTypeNameById(int id) {
		String result = null;
		try {
			String sql = "select * from type where typeid=?";
			Object[] params = new Object[] { id };
			ResultSet rs = manage.executeQuery(sql, params,
					DBConstants.PSTM_TYPE);
			rs.next();
			result = rs.getString(DBConstants.TYPE_NAME);
			manage.closeDB();
		} catch (Exception e) {
			ErrorManager.printError("GoodsTypeDao.getGoodsTypeNameByIde", e);
		}
		return result;
	}
	
	public boolean deleteGoodsType(String id) {
		boolean result = false;
		try {
			String sql = "delete from type where typeid=?";
			Object[] params = new Object[] { id };
			result = manage.executeUpdate(sql, params, DBConstants.PSTM_TYPE);
			manage.closeDB();
		} catch (Exception e) {
			ErrorManager.printError("GoodsTypeDao.deleteGoodsType", e);
		}
		return result;
	}
}
